<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="id" width="60" align="center"></el-table-column>
      <el-table-column prop="orderNumber" label="订单编号" width="150" align="center"></el-table-column>
      <el-table-column prop="detailId" label="手机详情id" width="100" align="center"></el-table-column>
      <el-table-column label="手机图片" width="280">
        <template slot-scope="scope">
          <!--scope里面装着这一行所对应的对象信息,scope.$index得到下标
          scope.row得到这一行对应数组里面的图片路径-->
          <img :src="require('D:/files/'+scope.row.url)" width="100%" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="phoneId" label="手机id" align="center"></el-table-column>
      <el-table-column prop="phoneNumber" label="手机号" align="center"></el-table-column>
      <el-table-column prop="purchaseVolume" label="购买量" align="center"></el-table-column>
      <el-table-column prop="address" label="收货地址" align="center"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    openDeleteConfirm(order) {
      this.$confirm('即将永久删除【' + order.username + '】管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(order.id);
      }).catch(() => {
      });
    },
    handleEdit(id) {
      console.log('准备【编辑订单】，数据id为【' + id + '】……');
      this.$alert('当前功能【编辑管理员】尚未实现！', '操作无法完成', {
        confirmButtonText: '确定', type: 'warn', showClose: false
      });
    },
    handleDelete(id) {
      console.log('准备【删除订单】，数据id为【' + id + '】……');
      let url = 'http://localhost:9081/orders/' + id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url)
          .then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              console.log('删除成功！');
              this.$message({
                message: '删除成功！',
                type: 'success'
              });
            } else if (responseBody.state == 40400) {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
            } else {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
              return;
            }
            this.loadOrderList();
          })
    },
    loadOrderList() {
      console.log('准备从服务器端加载【订单列表】……');
      let url = 'http://localhost:9081/orders';
      console.log('请求路径：' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url)
          .then((response) => {
            this.tableData = response.data.data;
            console.log(this.tableData)
          });
    }
  },
  mounted() {
    console.log('mounted');
    this.loadOrderList();
  }
}
</script>

<style scoped>
*{
  background-color: white;
}
</style>